package com.itheima.web.controller.system.permission;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Permission;
import com.itheima.domain.system.SysLog;
import com.itheima.utils.BeanUtil;
import com.itheima.utils.LogUtil;
import com.itheima.web.controller.BaseServlet;
import com.itheima.web.controller.system.syslog.SysLogServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

// uri:/system/permission?operation=list
@WebServlet("/system/permission")
public class PermissionServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if("list".equals(operation)){
            this.list(request,response);
        }else if("toAdd".equals(operation)){
            this.toAdd(request,response);
        }else if("save".equals(operation)){
            this.save(request, response);
        }else if("delete".equals(operation)){
            this.delete(request,response);
        }else if ("deleteMany".equals(operation)){
            this.deleteMany(request,response);
        }
    }

    private void deleteMany(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<String> list = new ArrayList<>();
        String moduleIds = request.getParameter("moduleIds");
//        System.out.println(moduleIds);
        String[] str = moduleIds.split(",");
        for (int i = 0; i < str.length; i++) {
           list.add(str[i]);
            System.out.println(str[i]);
        }
        permissionService.deleteMany(list);
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");

        //记日志
        SysLogServlet.logMethodForServlet(request);

    }

    //遍历全部
    private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
        int currentPage = 1;
        int pageSize = 5;
        if(StringUtils.isNotBlank(request.getParameter("currentPage"))){
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
        }
        if(StringUtils.isNotBlank(request.getParameter("pageSize"))){
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }
        PageInfo all = permissionService.findAll(currentPage, pageSize);

        //将数据保存到指定的位置
        request.setAttribute("page",all);


        //记日志,list方法记日志在跳转之前
        SysLogServlet.logMethodForServlet(request);

        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-list.jsp").forward(request,response);



    }

    //跳转到添加
    private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //加载所有的部门信息放入到permissionList
        List<Permission> all = permissionService.findAll();
        request.setAttribute("permissionList",all);

        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-add.jsp").forward(request,response);

        //记日志
        SysLogServlet.logMethodForServlet(request);
    }

    //添加
    private void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {


        //将数据获取到，封装成一个对象
        Permission permission = BeanUtil.fillBean(request,Permission.class);
        //调用业务层接口save

        //System.out.println(permission);
        permissionService.save(permission);
        //跳转回到页面list

        response.sendRedirect(request.getContextPath()+"/system/permission?operation=list");

        //记日志
        SysLogServlet.logMethodForServlet(request);
    }
    //删除
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
//        Permission permission = BeanUtil.fillBean(request,Permission.class);
        String id = request.getParameter("id");
        //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
        permissionService.delete(id);

        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");

        //记日志
        SysLogServlet.logMethodForServlet(request);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
